package tsinghua.smxq.mc;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import tsinghua.smxq.ui.MCListViewListener;
import tsinghua.smxq.ui.MCMenuAdapter;
import tsinghua.smxq.ui.MCMenuView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class StockSearchActivity extends Activity {
	Button ok;
	private ImageView imageDaily, imageFen, imageWeekly, imageMonthly;
	EditText text1;
	TextView textView0, textView1, textView2, textView3, textView4, textView5,
			textView6, textView7, textView8, textView9, textView10, textView11,
			textView12, textView13, textView14, textView15, textView16,
			textView17, textView18, textView19, textView20, textView21,
			textView22, textView23, textView24, textView25, textView26,
			textView27, textView28, textView29, textView30, textView31;
	private final static int CONNECTION_TIMEOUT = 5000;
	private final static int SO_TIMEOUT = 30000;

	Button menuBtn;
	LayoutInflater inflater;
	public MCMenuView menuView;
	ListView menuList;
	View RightView;
	View[] children;
	MCMenuAdapter menuAdapter;
	MCListViewListener menuListener;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(this);
		setContentView(inflater.inflate(R.layout.menu_scroll_view, null));

		RightView = inflater.inflate(R.layout.stocksearch, null);

		menuView = (MCMenuView) findViewById(R.id.mScrollView);
		menuAdapter = new MCMenuAdapter(this);

		menuList = (ListView) findViewById(R.id.menuList);
		menuListener = new MCListViewListener(this);
		menuList.setAdapter(menuAdapter);
		menuList.setOnItemClickListener(menuListener);
		menuBtn = (Button) RightView.findViewById(R.id.stocks_menuBtn);
		menuBtn.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				StockSearchActivity.this.menuView.clickMenuBtn();
			}

		});

		ok = (Button) RightView.findViewById(R.id.buttonOK);
		imageDaily = (ImageView) RightView.findViewById(R.id.imageDaily);
		imageWeekly = (ImageView) RightView.findViewById(R.id.imageWeekly);
		imageMonthly = (ImageView) RightView.findViewById(R.id.imageMonthly);
		imageFen = (ImageView) RightView.findViewById(R.id.imageFen);
		text1 = (EditText) RightView.findViewById(R.id.editText1);
		textView0 = (TextView) RightView.findViewById(R.id.textView0);
		textView1 = (TextView) RightView.findViewById(R.id.textView1);
		textView2 = (TextView) RightView.findViewById(R.id.textView2);
		textView3 = (TextView) RightView.findViewById(R.id.textView3);
		textView4 = (TextView) RightView.findViewById(R.id.textView4);
		textView5 = (TextView) RightView.findViewById(R.id.textView5);
		textView6 = (TextView) RightView.findViewById(R.id.textView6);
		textView7 = (TextView) RightView.findViewById(R.id.textView7);
		textView8 = (TextView) RightView.findViewById(R.id.textView8);
		textView9 = (TextView) RightView.findViewById(R.id.textView9);
		textView10 = (TextView) RightView.findViewById(R.id.textView10);
		textView11 = (TextView) RightView.findViewById(R.id.textView11);
		textView12 = (TextView) RightView.findViewById(R.id.textView12);
		textView13 = (TextView) RightView.findViewById(R.id.textView13);
		textView14 = (TextView) RightView.findViewById(R.id.textView14);
		textView15 = (TextView) RightView.findViewById(R.id.textView15);
		textView16 = (TextView) RightView.findViewById(R.id.textView16);
		textView17 = (TextView) RightView.findViewById(R.id.textView17);
		textView18 = (TextView) RightView.findViewById(R.id.textView18);
		textView19 = (TextView) RightView.findViewById(R.id.textView19);
		textView20 = (TextView) RightView.findViewById(R.id.textView20);
		textView21 = (TextView) RightView.findViewById(R.id.textView21);
		textView22 = (TextView) RightView.findViewById(R.id.textView22);
		textView23 = (TextView) RightView.findViewById(R.id.textView23);
		textView24 = (TextView) RightView.findViewById(R.id.textView24);
		textView25 = (TextView) RightView.findViewById(R.id.textView25);
		textView26 = (TextView) RightView.findViewById(R.id.textView26);
		textView27 = (TextView) RightView.findViewById(R.id.textView27);
		textView28 = (TextView) RightView.findViewById(R.id.textView28);
		textView29 = (TextView) RightView.findViewById(R.id.textView29);
		textView30 = (TextView) RightView.findViewById(R.id.textView30);
		textView31 = (TextView) RightView.findViewById(R.id.textView31);

		OnClickListener listener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					String str = text1.getText().toString();
					str.trim();

					Bitmap bm = getBitmapFromUrl("http://image.sinajs.cn/newchart/daily/n/"
							+ str + ".gif");

					imageDaily.setImageBitmap(bm);
					bm = getBitmapFromUrl("http://image.sinajs.cn/newchart/min/n/"
							+ str + ".gif");

					imageFen.setImageBitmap(bm);
					bm = getBitmapFromUrl("http://image.sinajs.cn/newchart/weekly/n/"
							+ str + ".gif");

					imageWeekly.setImageBitmap(bm);
					bm = getBitmapFromUrl("http://image.sinajs.cn/newchart/monthly/n/"
							+ str + ".gif");

					imageMonthly.setImageBitmap(bm);

					String s = getStringFromUrl("http://hq.sinajs.cn/list="
							+ str);
					String[] ss = s.split("\"");
					ss = ss[1].split(",");
					textView0.setText("股票名称:" + ss[0]);
					textView1.setText("今日开盘价:" + ss[1]);
					textView2.setText("昨日收盘价:" + ss[2]);
					textView3.setText("当前价格:" + ss[3]);
					textView4.setText("今日最高价:" + ss[4]);
					textView5.setText("今日最低价:" + ss[5]);
					textView6.setText("竞买价:" + ss[6]);
					textView7.setText("竞卖价:" + ss[7]);
					textView8.setText("成交的股票数:" + ss[8]);
					textView9.setText("成交金额:" + ss[9]);
					textView10.setText("买一:" + ss[10]);
					textView11.setText("买一报价:" + ss[11]);
					textView12.setText("买二:" + ss[12]);
					textView13.setText("买二报价:" + ss[13]);
					textView14.setText("买三:" + ss[14]);
					textView15.setText("买三报价:" + ss[15]);
					textView16.setText("买四:" + ss[16]);
					textView17.setText("买四报价:" + ss[17]);
					textView18.setText("买五:" + ss[18]);
					textView19.setText("买五报价:" + ss[19]);
					textView20.setText("卖一:" + ss[20]);
					textView21.setText("卖一报价:" + ss[21]);
					textView22.setText("卖二:" + ss[22]);
					textView23.setText("卖二报价:" + ss[23]);
					textView24.setText("卖三:" + ss[24]);
					textView25.setText("卖三报价:" + ss[25]);
					textView26.setText("卖四:" + ss[26]);
					textView27.setText("卖四报价:" + ss[27]);
					textView28.setText("卖五:" + ss[28]);
					textView29.setText("卖五报价:" + ss[29]);
					textView30.setText("日期:" + ss[30]);
					textView31.setText("时间:" + ss[31]);

				} catch (HttpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch blockF
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		};

		ok.setOnClickListener(listener);
		View leftView = new View(this);
		children = new View[] { leftView, RightView };
		leftView.setBackgroundColor(Color.TRANSPARENT);
		this.menuView.initViews(children, this.menuBtn, this.menuList);
		this.menuView.setMenuBtn(this.menuBtn);
		MCMenuAdapter.currPosition = 5;

	}

	private String getStringFromUrl(String url) throws Exception {

		HttpMethod method = new GetMethod(url);
		HttpClient mHttpClient = new HttpClient();
		mHttpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(CONNECTION_TIMEOUT);
		mHttpClient.getHttpConnectionManager().getParams()
				.setSoTimeout(SO_TIMEOUT);

		int statusCode = mHttpClient.executeMethod(method);
		if (statusCode != HttpStatus.SC_OK) {
			method.releaseConnection();
			return null;
		}

		InputStream is = method.getResponseBodyAsStream();
		InputStreamReader reader = new InputStreamReader(
				new BufferedInputStream(is), Charset.forName("gbk"));
		BufferedReader bReader = new BufferedReader(reader);

		String s = inputStream2String(is);
		return s;
	}

	String inputStream2String(InputStream is) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new BufferedInputStream(is), Charset.forName("gbk")));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		while ((line = in.readLine()) != null) {
			buffer.append(line);
		}
		return buffer.toString();
	}

	private Bitmap getBitmapFromUrl(String url) throws HttpException,
			IOException {

		HttpMethod method = new GetMethod(url);
		HttpClient mHttpClient = new HttpClient();
		mHttpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(CONNECTION_TIMEOUT);
		mHttpClient.getHttpConnectionManager().getParams()
				.setSoTimeout(SO_TIMEOUT);

		int statusCode = mHttpClient.executeMethod(method);
		if (statusCode != HttpStatus.SC_OK) {
			method.releaseConnection();
			return null;
		}

		InputStream in = method.getResponseBodyAsStream();
		BufferedInputStream bis = new BufferedInputStream(in);

		Bitmap bm = BitmapFactory.decodeStream(bis);

		bis.close();
		method.releaseConnection();

		return bm;
	}

}
